package stz.flash.main;

import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.component.BitmapField;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.decor.BackgroundFactory;
import stz.flash.Info;
import stz.flash.main.state.KeyOperator;
import stz.flash.option.Option;
import blackberry.util.field.DjChangeableBitmapField;
import blackberry.util.field.DjHighlightManager;
import blackberry.util.field.DjLabelField;

public class TitleManager extends Manager implements FieldChangeListener{

	private static final int INDEX_OF_ENABLE_BIMAP = 0;
	private static final int INDEX_OF_DISABLE_BITMAP = 1;

	//Bitmap
	private static Bitmap intoFlashBitmap_enable = Bitmap.getBitmapResource("into_Flashlight_enable.png");
	private static Bitmap intoFlashBitmap_disable = Bitmap.getBitmapResource("into_Flashlight_disable.png");
	private static Bitmap intoStrobeBitmap_enable = Bitmap.getBitmapResource("into_StrobeMode_enable.png");
	private static Bitmap intoStrobeBitmap_disable = Bitmap.getBitmapResource("into_StrobeMode_disable.png");
	
	private static Bitmap leftArrowBitmap = Bitmap.getBitmapResource("LeftArrow.png");
	private static Bitmap rightArrowBitmap = Bitmap.getBitmapResource("RightArrow.png");
	
	//Field
	private static TitleHighlightManager intoFlashManager = new TitleHighlightManager();
	private static BitmapField leftArrowField = new BitmapField(leftArrowBitmap);
	private static DjChangeableBitmapField intoFlashField = new DjChangeableBitmapField(
			new Bitmap[] {intoFlashBitmap_enable, intoFlashBitmap_disable}, 
			FOCUSABLE);

	private static TitleHighlightManager intoStrobeManager = new TitleHighlightManager();
	private static BitmapField rightArrowField = new BitmapField(rightArrowBitmap);
	private static DjChangeableBitmapField intoStrobeField = new DjChangeableBitmapField(
			new Bitmap[] {intoStrobeBitmap_enable, intoStrobeBitmap_disable},
			FOCUSABLE);
	
	private static DjLabelField C_hotkeyAdvice = HotkeyAdviceManager.createHotkeyLabel("C");
	
	private static TitleManager instance = new TitleManager();
	
	private TitleManager() {
		super(0);
		
		setBackground(BackgroundFactory.createSolidTransparentBackground(
				Color.GRAY, 80));
		
		intoFlashManager.add(leftArrowField);
		intoFlashManager.add(intoFlashField);
		add(intoFlashManager);
		
		intoStrobeManager.add(intoStrobeField);
		intoStrobeManager.add(rightArrowField);
		add(intoStrobeManager);
		
	}
	
	public static TitleManager getIns(){
		return instance;
	}
	
	public void fieldChanged(Field field, int context) {
		ModeChangeHandler.changeIntoStrobeMode();
	}
	
	protected void sublayout(int width, int height) {
	
		setExtent(width, getPreferredHeight());
		
		setPositionChild(intoFlashManager, 
				0, 
				0);
		layoutChild(intoFlashManager, 
				intoFlashManager.getPreferredWidth(), 
				intoFlashManager.getPreferredHeight());
		
		
		setPositionChild(intoStrobeManager, 
				Display.getWidth() - intoStrobeManager.getPreferredWidth(), 
				0);
		layoutChild(intoStrobeManager, 
				intoStrobeManager.getPreferredWidth(), 
				intoStrobeManager.getPreferredHeight());
		
		
		if (C_hotkeyAdvice.getIndex() != -1) {
			
			if (Option.getIns().getLatestMode() == Option.FLASHLIGHT_MODE) {
				setPositionChild(C_hotkeyAdvice, 
						intoStrobeManager.getLeft() 
								- getPreferredWidthOfChild(C_hotkeyAdvice), 
						0);
			} else {
				setPositionChild(C_hotkeyAdvice, 
						intoFlashManager.getLeft() + intoFlashManager.getPreferredWidth(), 
						0);
			}
			layoutChild(C_hotkeyAdvice, 
					getPreferredWidthOfChild(C_hotkeyAdvice),
					getPreferredHeightOfChild(C_hotkeyAdvice));
		}
	}
	
	public static void disableIntoStrobeManager() {
		
		intoStrobeManager.setEnabled(false);
		intoStrobeField.changeBitmapField(INDEX_OF_DISABLE_BITMAP);
		
		intoFlashManager.setEnabled(true);
		intoFlashField.changeBitmapField(INDEX_OF_ENABLE_BIMAP);
	}
	public static void disableIntoFlashManager() {
		
		intoFlashManager.setEnabled(false);
		intoFlashField.changeBitmapField(INDEX_OF_DISABLE_BITMAP);
		
		intoStrobeManager.setEnabled(true);
		intoStrobeField.changeBitmapField(INDEX_OF_ENABLE_BIMAP);
	}
	
	public int getPreferredHeight() {
		return intoFlashManager.getTop() + intoFlashManager.getPreferredHeight();
	}
	public int getPreferredWidth() {
		return Display.getWidth();
	}
	
	public void addCLabel(){
		if (C_hotkeyAdvice.getIndex() != -1)
			return;
		add(C_hotkeyAdvice);
	}
	public void delCLabel(){
		if (C_hotkeyAdvice.getIndex() == -1)
			return;
		delete(C_hotkeyAdvice);
	}
	
	static class TitleHighlightManager extends DjHighlightManager {

		public TitleHighlightManager() {
			super(Info.COLOR_OF_FOCUS, 
					Info.GLOBAL_ALPHA_OF_HIGHLIGHT,
					new HorizontalFieldManager());
		}
		
		public void execute() {
			KeyOperator.runC();
		};
	}
}